package com.yvon.maple.service.msg.core.controller;

import com.yvon.boot.common.result.ResponseData;
import com.yvon.boot.mybatis.page.ListData;
import com.yvon.boot.mybatis.page.SearchCriteria;
import com.yvon.boot.mybatis.search.PageQuery;
import com.yvon.maple.cloud.web.data.AbstractController;
import com.yvon.maple.cloud.web.filter.LoginUserUtil;
import com.yvon.maple.service.msg.core.model.dto.ImContentDTO;
import com.yvon.maple.service.msg.core.model.entity.ImContent;
import com.yvon.maple.service.msg.core.service.ImConsumerService;
import com.yvon.maple.service.msg.core.service.ImContentService;
import com.yvon.maple.service.msg.core.model.vo.ImContentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author : Yvon
 * @since : 2021-08-26
 */
@RestController
@RequestMapping("/im")
@Api(value = "IM站内消息", tags = "消息服务 - IM站内消息API")
public class ImController extends AbstractController<ImContent, ImContentDTO, ImContentVo, ImContentService> {

    /**
     * Message service
     */
    @Autowired
    private ImContentService imContentService;

    /**
     * Message consumer service
     */
    @Autowired
    private ImConsumerService imConsumerService;

    /**
     * 设置已读
     *
     * @param consumerIds 消费者ID集合
     * @return {@link ResponseData }
     * @author : Yvon / 2021-09-02
     */
    @PutMapping("/mark/read")
    @ApiOperation(value = "设置已读", notes = "设置已读API")
    public ResponseData<Boolean> markRead(@RequestBody Set<Long> consumerIds) {
        return success(imConsumerService.markRead(consumerIds));
    }

    /**
     * 获取当前用户未读消息数量.
     *
     * @return {@link ResponseData<Long> }
     * @author : Yvon / 2021-09-02
     */
    @PostMapping("/unread/count")
    @ApiOperation(
            value = "获取当前用户未读消息数量",
            notes = "获取当前用户未读消息数量API"
    )
    public ResponseData<Long> getUnReadCount() {
        return success(imContentService.getUnReadCount(LoginUserUtil.getUsername()));
    }



    @GetMapping({"/page"})
    @ApiOperation(
            value = "分页查询消息",
            notes = "分页查询消息API"
    )
    public ResponseData<ListData<ImContentDTO>> pageList(PageQuery pageQuery) {
        ListData listData = imContentService.page(pageQuery);
        listData.setList(this.entityToDto(listData.getList()));
        return success(listData);
    }
}
